<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label-width="100px" label="业务包名称"  prop="name">
      <el-input v-model="dataForm.name"  placeholder="业务包名称"></el-input>
    </el-form-item>

    <el-form-item label-width="100px" label="业务包描述"  prop="description">
      <el-input v-model="dataForm.description"  placeholder="业务包描述"></el-input>
    </el-form-item>

    <el-form-item label="分配用户" >

     <el-select v-model="selectedUser" multiple @change="userOnChanged($event)" collapse-tags style="margin-left: 20px;" placeholder="请选择">
        <el-option
          v-for="item in userList"
          :key="item.value"
          :label="item.label"
         
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>

    <!-- <el-form-item label="创建者ID" prop="createrId">
      <el-input v-model="dataForm.createrId" placeholder="创建者ID"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          description: '',
          createrId: ''
        },
        connectId: '',
       userList: [],
        selectedUser: [],
        dataRule: {
          name: [
            { required: true, message: '业务包名称不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '业务包描述不能为空', trigger: 'blur' }
          ],
          createrId: [
            { required: true, message: '创建者ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
            userOnChanged(e){
        this.selectedUser=e;
      },
      getAllUsers(){
        this.$http({
          url: this.$http.adornUrl('/report/connection/users'),
          method: 'get',
          params: this.$http.adornParams(
          )
        }).then(({data}) => {
              this.userList=data.userList;
        })
      },
      init (id,connectId) {
        this.connectId=connectId;
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
           this.getAllUsers();
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/report/businesspackage/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.businessPackage.name
                this.dataForm.createrId = data.businessPackage.createrId
                 this.dataForm.description = data.businessPackage.description
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/report/businesspackage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'description': this.dataForm.description,
                'createrId': this.$store.state.user.id,
                'userIds': this.selectedUser,
                'connectionId': this.connectId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
